<?php

namespace App\Http\Controllers\Api\V6;

use App\Libs\BaiDuYun\Live;
use App\Models\V1\Comment;
use App\Models\V1\MemberModel;
use App\Repository\WxRegister;
use App\Repository\WxUserInfoRegister;
use BaiduBce\Services\Bos\BosClient;
use BaiduBce\Services\Lss\LssClient;
use Carbon\Carbon;
use EasyWeChat\Foundation\Application;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Session;

class ShareController extends Controller
{
    use WxUserInfoRegister;

    public function user_info(Request $request)
    {
        $this->register();
        return redirect(session('wxURL'));
    }

    public function wxUserAddComment(Request $request)
    {
        $is_sub = $request->is_sub;
        $userInfo = Session::get('wxUserInfo');

        $pid = $request->pid;
        $replay_user_id = $request->replay_user_id;
        $body = $request->body;
        $lessons_id = $request->lessons_id;
        $type = $request->type;
        $userInfo_id = isset($userInfo['id']) ? $userInfo['id'] : $userInfo['UserID'];

        $patterns = [//过滤用户的输入
            '/猪*/',
            '/傻*/',
            '/666*/'
        ];
        $body = preg_replace($patterns, '***', $body);

        $comment = Comment::create([
            'user_id' => $userInfo_id,
            'pid' => $pid,
            'replay_user_id' => $replay_user_id,
            'body' => $body,
            'lessons_id' => $lessons_id,
            'type' => $type
        ]);
        $data = DB::table('comments as c')
            ->select('c.*', 'm.YourName', 'm.FilePath1', 'mm.YourName as replayName')
            ->where('c.id', '=', $comment->id)
            ->join('memberinfo as m', 'm.UserID', '=', 'c.user_id')
            ->leftJoin('memberinfo as mm', 'mm.UserID', '=', 'c.replay_user_id')
            ->first();
        foreach ($data as $k => &$v) {
            $data->FilePath1 = change_member_head_img($data->FilePath1);
            $data->show_time = Carbon::parse($data->created_at)->diffForHumans();
        }
        return \response(['code' => 200, 'is_sub' => $is_sub, 'pid' => $pid, 'msg' => 'success', 'data' => $data], 200);
    }
}























